package pkg

import (
	"context"
	"github.com/qiniu/go-sdk/v7/storagev2/credentials"
	"github.com/qiniu/go-sdk/v7/storagev2/http_client"
	"github.com/qiniu/go-sdk/v7/storagev2/uploader"
	"hotel/appcofig"
	"mime/multipart"
)

func QiNiuYunUpload(file multipart.File, fileName string) (string, error) {
	Qny := appcofig.AppConf.QiUinYunConfig
	accessKey := Qny.AccessKey
	secretKey := Qny.SecretKey
	mac := credentials.NewCredentials(accessKey, secretKey)
	bucket := Qny.Bucket
	key := fileName
	uploadManager := uploader.NewUploadManager(&uploader.UploadManagerOptions{
		Options: http_client.Options{
			Credentials: mac,
		},
	})
	err := uploadManager.UploadReader(context.Background(), file, &uploader.ObjectOptions{
		BucketName: bucket,
		ObjectName: &key,
		FileName:   key,
		CustomVars: map[string]string{
			"name": "github logo",
		},
	}, nil)
	if err != nil {
		return "", err
	}
	return Qny.Url + key, err
}
